import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/check_in_detail/api.dart";
import "package:ct_pa_majordomo/pages/widgets/textarea_form_field.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class EditRemark extends StatefulWidget {
  final String remark;
  final int checkInId;
  final Function changeInfo;
  const EditRemark({super.key, required this.remark, required this.checkInId, required this.changeInfo});

  @override
  State<StatefulWidget> createState() => _EditRemarkState();

}

class _EditRemarkState extends State<EditRemark> {

  String _remark = "";

  @override
  void initState() {
    setState(() {
      _remark = widget.remark;
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "编辑备注",
      child: ListView(
        padding: const EdgeInsets.all(ThemeVars.paddingPage),
        children: [
          TextAreaFormField(
            initialValue: _remark,
            maxLength: 150,
            onChanged: (value) => setState(() {
              _remark = value;
            }),
          ),
          const SizedBox(height: 30,),
          Button(
              onPressed: _remark.isNotEmpty ? () async {
                Map<String, dynamic> params = {
                  "checkInId": widget.checkInId,
                  "remark": _remark,
                };
                EasyLoading.show();
                await checkInApi.updateCheckIn(params);
                widget.changeInfo();
                EasyLoading.dismiss();
                if (context.mounted) {
                  Navigator.pop(context);
                }
              } : null,
              child: Text("保存")
          )
        ],
      ),
    );
  }

}